/*
 * $Id: ActTaskFtRecordDAO.java,v 1.7 2009/10/13 16:25:09 oliver Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActTaskFtRecord;
import com.ge.healthcare.autosc.common.database.pojo.ActTaskFtRecordId;

/**
 * @author 212042764
 * 
 */
public class ActTaskFtRecordDAO extends BaseDAO implements ITaskFtRecordDAO {
	
	/**
	 * Eclispe generated
	 */
	private static final long serialVersionUID = -2008286765356095565L;

	public ActTaskFtRecordDAO() {
		super(ActTaskFtRecord.TABLE_NAME);
	}

	/*
	 * (non-Javadoc)
	 * getFileTransferStatusByTaskId
	 * @seecom.ge.healthcare.autosc.common.database.dao.ITaskFtRecordDAO#
	 * getFileTransferRecordById(java.lang.Integer, java.lang.Integer)
	 */
	public ActTaskFtRecord getFileTransferRecordById(Long taskId, Long ftId) {
		Map<String, Object> whereClause = new HashMap<String, Object>();
		whereClause.put(ActTaskFtRecord.COL_TASK_ID, taskId);
		whereClause.put(ActTaskFtRecord.COL_FT_ID, ftId);
		List<?> rtvdFTRecord = this.findByProperties(whereClause);
		if (ASCUtil.isNullOrEmptyList(rtvdFTRecord)) {
			return null;
		}
		return (ActTaskFtRecord) rtvdFTRecord.get(0);
	}

	public Map<Long, Boolean> getFileTransferStatusByTaskId(Long taskId) {
		List<?> rtvdFTRecords = this.findByProperty(ActTaskFtRecord.COL_TASK_ID, taskId);
		if (ASCUtil.isNullOrEmptyList(rtvdFTRecords)) {
			return null;
		}
		Map<Long, Boolean> map = new HashMap<Long, Boolean>();
		for (Object record : rtvdFTRecords) {
			ActTaskFtRecord ftRecord = (ActTaskFtRecord) record;
			map.put(ftRecord.getId().getFtId(), ftRecord
					.hasSuccessfullyBeenTransferred());
		}
		return map;
	}

	public List<ActTaskFtRecord> getFileTransferRecordsByTaskId(Long taskId) {
		List<?> rtvdFTRecords = this.findByProperty(ActTaskFtRecord.COL_TASK_ID, taskId);
		if (ASCUtil.isNullOrEmptyList(rtvdFTRecords)) {
			return null;
		}
		List<ActTaskFtRecord> ftRecords = new ArrayList<ActTaskFtRecord>();
		for (Object record : rtvdFTRecords) {
			ftRecords.add((ActTaskFtRecord) record);
		}
		return ftRecords;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ge.healthcare.autosc.common.database.dao.ITaskFtRecordDAO#saveAlways
	 * (java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	public void saveSuccess(Long ftId, Long taskId, String fileName) {
		this.saveSuccessOrFailure(ftId, taskId, fileName, true);
	}

	public void saveFailure(Long ftId, Long taskId, String fileName) {
		this.saveSuccessOrFailure(ftId, taskId, fileName, false);
	}

	/**
	 * Saves the record to the database
	 * 
	 * @param ftId
	 * @param taskId
	 * @param fileName
	 * @param success
	 */
	private void saveSuccessOrFailure(Long ftId, Long taskId, String fileName,
			boolean success) {
		// The get has been added to avoid hibenate cribbing that there is an
		// object
		// already associated in the session
		ActTaskFtRecord ftRecord = this.getFileTransferRecordById(taskId, ftId);

		if (ASCUtil.isNullOrEmptyObject(ftRecord)) {
			ftRecord = new ActTaskFtRecord();
			ActTaskFtRecordId recordId = new ActTaskFtRecordId();
			recordId.setFtId(ftId);
			recordId.setTaskId(taskId);
			ftRecord.setId(recordId);
		}
		ftRecord.setFileUrl(fileName);
		if (success) {
			ftRecord.setCodeName(ActTaskFtRecord.SUCCESS_CODE_NAME);
		} else {
			ftRecord.setCodeName(ActTaskFtRecord.FAILURE_CODE_NAME);
		}
		super.save(ftRecord);
	}
}
